<<<<<<< HEAD
#include <stdlib.h>
#include <cstdio>
#include <stdint.h>
#include "sys_log.h"
#include "sys_common.h"

int main(int argc, char* argv[])
{
	LogConfig config = {
        .level = LOG_LVL_NOTE,
        .target = LOG_TARGET_CONSOLE,
        .use_color = 1
    };
    log_init(config);

	{
	/*******************coding*************************/
	LOG_NOTE("NV12转UYVY\n");
    // 假设图片分辨率（需根据实际图片尺寸修改）
    const int width = 720;
    const int height = 576;

    // 打开NV12文件
    FILE *fp_nv12 = fopen("test_720_576.yuv", "rb");
    if (!fp_nv12) {
        perror("Failed to open test_720_576.yuv");
        return 1;
    }

    // 计算NV12文件大小
    const size_t nv12_size = width * height * 3 / 2;
    uint8_t *nv12_data = (uint8_t*)malloc(nv12_size);
    if (!nv12_data) {
        perror("Memory allocation failed");
        fclose(fp_nv12);
        return 1;
    }

    // 读取NV12数据
    if (fread(nv12_data, 1, nv12_size, fp_nv12) != nv12_size) {
        perror("Failed to read NV12 data");
        fclose(fp_nv12);
        free(nv12_data);
        return 1;
    }
    fclose(fp_nv12);

    // 分配UYVY缓冲区（每个像素2字节）
    const size_t uyvy_size = width * height * 2;
    uint8_t *uyvy_data = (uint8_t*)malloc(uyvy_size);
    if (!uyvy_data) {
        perror("Memory allocation failed");
        free(nv12_data);
        return 1;
    }

    // 获取Y和UV平面指针
    uint8_t *y_plane = nv12_data;
    uint8_t *uv_plane = nv12_data + width * height;

    // NV12转UYVY
    for (int y = 0; y < height; y++) {
        // 当前行在UV平面的行号（每两行Y共享一行UV）
        int uv_row = y / 2;
        for (int x = 0; x < width; x += 2) {
            // 获取UV分量（每对UV覆盖2x2像素块）
            int uv_index = uv_row * width + x;
            uint8_t u = uv_plane[uv_index];
            uint8_t v = uv_plane[uv_index + 1];

            // 获取Y分量
            int y_index = y * width + x;
            uint8_t y0 = y_plane[y_index];
            uint8_t y1 = y_plane[y_index + 1];

            // 计算UYVY输出位置（每两个像素占4字节）
            int uyvy_index = (y * width + x) * 2;
            
            // 写入UYVY数据（格式: U0, Y0, V0, Y1）
            uyvy_data[uyvy_index]     = u;
            uyvy_data[uyvy_index + 1] = y0;
            uyvy_data[uyvy_index + 2] = v;
            uyvy_data[uyvy_index + 3] = y1;
        }
    }

    // 写入UYVY文件
    FILE *fp_uyvy = fopen("Output_720_576.uyvy", "wb");
    if (!fp_uyvy) {
        perror("Failed to create Output_720_576.uyvy");
        free(nv12_data);
        free(uyvy_data);
        return 1;
    }

    if (fwrite(uyvy_data, 1, uyvy_size, fp_uyvy) != uyvy_size) {
        perror("Failed to write UYVY data");
    }

    // 清理资源
    fclose(fp_uyvy);
    free(nv12_data);
    free(uyvy_data);

    printf("Conversion completed successfully.\n");
    return 0;

    /**************************************************/
	}

	log_close();
	return OK;
=======
#include <stdlib.h>
#include <cstdio>
#include <stdint.h>
#include "sys_log.h"
#include "sys_common.h"

int main(int argc, char* argv[])
{
	LogConfig config = {
        .level = LOG_LVL_NOTE,
        .target = LOG_TARGET_CONSOLE,
        .use_color = 1
    };
    log_init(config);

	{
	/*******************coding*************************/
	LOG_NOTE("NV12转UYVY\n");
    // 假设图片分辨率（需根据实际图片尺寸修改）
    const int width = 720;
    const int height = 576;

    // 打开NV12文件
    FILE *fp_nv12 = fopen("test_720_576.yuv", "rb");
    if (!fp_nv12) {
        perror("Failed to open test_720_576.yuv");
        return 1;
    }

    // 计算NV12文件大小
    const size_t nv12_size = width * height * 3 / 2;
    uint8_t *nv12_data = (uint8_t*)malloc(nv12_size);
    if (!nv12_data) {
        perror("Memory allocation failed");
        fclose(fp_nv12);
        return 1;
    }

    // 读取NV12数据
    if (fread(nv12_data, 1, nv12_size, fp_nv12) != nv12_size) {
        perror("Failed to read NV12 data");
        fclose(fp_nv12);
        free(nv12_data);
        return 1;
    }
    fclose(fp_nv12);

    // 分配UYVY缓冲区（每个像素2字节）
    const size_t uyvy_size = width * height * 2;
    uint8_t *uyvy_data = (uint8_t*)malloc(uyvy_size);
    if (!uyvy_data) {
        perror("Memory allocation failed");
        free(nv12_data);
        return 1;
    }

    // 获取Y和UV平面指针
    uint8_t *y_plane = nv12_data;
    uint8_t *uv_plane = nv12_data + width * height;

    // NV12转UYVY
    for (int y = 0; y < height; y++) {
        // 当前行在UV平面的行号（每两行Y共享一行UV）
        int uv_row = y / 2;
        for (int x = 0; x < width; x += 2) {
            // 获取UV分量（每对UV覆盖2x2像素块）
            int uv_index = uv_row * width + x;
            uint8_t u = uv_plane[uv_index];
            uint8_t v = uv_plane[uv_index + 1];

            // 获取Y分量
            int y_index = y * width + x;
            uint8_t y0 = y_plane[y_index];
            uint8_t y1 = y_plane[y_index + 1];

            // 计算UYVY输出位置（每两个像素占4字节）
            int uyvy_index = (y * width + x) * 2;
            
            // 写入UYVY数据（格式: U0, Y0, V0, Y1）
            uyvy_data[uyvy_index]     = u;
            uyvy_data[uyvy_index + 1] = y0;
            uyvy_data[uyvy_index + 2] = v;
            uyvy_data[uyvy_index + 3] = y1;
        }
    }

    // 写入UYVY文件
    FILE *fp_uyvy = fopen("Output_720_576.uyvy", "wb");
    if (!fp_uyvy) {
        perror("Failed to create Output_720_576.uyvy");
        free(nv12_data);
        free(uyvy_data);
        return 1;
    }

    if (fwrite(uyvy_data, 1, uyvy_size, fp_uyvy) != uyvy_size) {
        perror("Failed to write UYVY data");
    }

    // 清理资源
    fclose(fp_uyvy);
    free(nv12_data);
    free(uyvy_data);

    printf("Conversion completed successfully.\n");
    return 0;

    /**************************************************/
	}

	log_close();
	return OK;
>>>>>>> 39e3ba406c849a6599ada4a566e867047f276b22
}